package com.dreamwhale.machinery.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dreamwhale.machinery.config.DataResult;
import com.dreamwhale.machinery.dao.DriveTypesMapper;
import com.dreamwhale.machinery.entity.DriveTypes;
import com.dreamwhale.machinery.goods.entity.DriverInfo;
import com.dreamwhale.machinery.goods.mapper.DriverInfoMapper;
import com.dreamwhale.machinery.goods.service.DriverInfoService;
import com.dreamwhale.machinery.user.entity.YxUser;
import com.dreamwhale.machinery.user.mapper.YxUserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 司机招聘 服务实现类
 * </p>
 *
 * @author Author
 * @since 2020-05-28
 */
@Service
public class DriverInfoServiceImpl extends ServiceImpl<DriverInfoMapper, DriverInfo> implements DriverInfoService {


    @Resource
    private DriverInfoMapper driverInfoMapper;

    @Resource
    private YxUserMapper yxUserMapper;

    @Resource
    private DriveTypesMapper driveTypesMapper;

    @Override
    public int insertDriverInfos(DriverInfo driverInfo) {
        driverInfo.setCreateTime(LocalDateTime.now());
        return driverInfoMapper.insert(driverInfo);
    }

    @Override
    public List<DriverInfo> selectDriverInfoMap(Integer userId) {
        QueryWrapper<DriverInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).eq("del_flag", 0);
        return driverInfoMapper.selectList(queryWrapper);
    }

    @Override
    public List<DriverInfo> selectDriverInfos(Integer infoId) {
        QueryWrapper<DriverInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flag", 0).eq("id", infoId);
        List<DriverInfo> list = driverInfoMapper.selectList(queryWrapper);
        if (list != null && list.size() != 0) {
            for (DriverInfo driverInfo : list) {
                QueryWrapper<DriveTypes> queryWrappers = new QueryWrapper<>();
                queryWrappers.eq("del_flag", 0).eq("id", driverInfo.getDriveTypeId());
                DriveTypes driveTypes = driveTypesMapper.selectOne(queryWrappers);
                driverInfo.setDriveTypeName(driveTypes.getDriveType());
            }
        }

        return list;
    }

    @Override
    public DriverInfo selectDriverInfoBytypeId(Integer infoId) {

        return driverInfoMapper.selectDriverInfoBytypeId(infoId);
    }

    @Override
    public List<DriverInfo> selectDriverInfoHome(Integer salaryMin, Integer salaryMax, Integer years, String province,
                                                 String city, Integer type, Integer types, String title, String driveTypeId) {
        //找司机
        List<DriverInfo> driverInfo = driverInfoMapper.selectDriverInfoHome(salaryMin, salaryMax, years, province, city, type, types, title, driveTypeId);
        if (driverInfo != null) {
            for (DriverInfo machineBuy1 : driverInfo) {
                QueryWrapper<YxUser> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("uid", machineBuy1.getUserId());
                YxUser listy = yxUserMapper.selectOne(queryWrapper);
                machineBuy1.setNickname(listy.getNickname());
                machineBuy1.setAvatar(listy.getAvatar());
                QueryWrapper<DriveTypes> queryWrappers = new QueryWrapper<>();
                queryWrappers.eq("del_flag", 0).eq("drive_type", machineBuy1.getDriveTypeId());
                DriveTypes driveTypes = driveTypesMapper.selectOne(queryWrappers);
                if (driveTypes != null) {
                    machineBuy1.setDriveTypeName(driveTypes.getDriveType());
                }
            }
        }
        return driverInfo;
    }
}
